package com.manage.controller;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.manage.model.User;
import com.manage.service.UserService;
import com.manage.util.JSONutils;

@Controller
public class UserController {
	private UserService userService;
	public UserService getUserService() {
		return userService;
	}
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	//显示用户列表
	@RequestMapping(value="listuser")
	public String listUser(Map<String,Object> map){
		List<User> userList = userService.listAllUsers();
		if(userList==null){
			System.out.print("输出数据为空");
		}
		map.put("userList", userList);
		return "views/user";
	}
	
	//删除用户信息
	@RequestMapping(value="deleteuser")
	public void deleteUser(@RequestParam int userId,PrintWriter out){
		System.out.print(userId);
		userService.deleteUser(userId);
		out.write("success");
		out.close();
}
	
	//增加用户
	@RequestMapping(value="saveuser",method=RequestMethod.POST)
	public ModelAndView saveUser(User user){
		ModelAndView mv = new ModelAndView("jsonView");
		if(userService.addUser(user)==false){
			mv.addObject("msg","failed");
		}else{
			mv.addObject("msg","success");
		}
		mv.setViewName("views/save_result");
		return mv;
	}
	
	//登陆
	@RequestMapping(value = "login")
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		Map<String, Object> model = new HashMap<String, Object>();
		//判断用户是否存在
		if (userService.selectUser(user) != null) {
			user = userService.selectUser(user);
			request.getSession().setAttribute("username",user.getUsername());
			model.put("user", user);
			//根据用户ID对应的角色ID查找菜单列表
			
			return new ModelAndView("view/index", model);
		} else {
			model.put("error", "用户名或密码错误");
//			return new ModelAndView("views/login", model);
			return new ModelAndView("views/login2", model);
		}

	}
	
	//退出
	@RequestMapping(value = "exit")
	public ModelAndView eixtLogin(HttpServletRequest request, ModelMap map) {
		Map<String, Object> model = new HashMap<String, Object>();
//		return new ModelAndView("views/login", model);
		return new ModelAndView("views/login2", model);
	}
	
	//修改
	@RequestMapping(value = "updateuser")
	public ModelAndView updateUser(HttpServletRequest request,@RequestParam(value="userid") int userid) {
		ModelAndView mav =new ModelAndView("jsonView");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		User user = new User();
		user.setUserid(userid);
		user.setUsername(username);
		user.setPassword(password);
		userService.updateByPrimaryKey(user);
		mav.setViewName("listuser.do?userid=${user.userid}");
		return mav;
	}
	
	//获取用户信息
	@RequestMapping(value = "getUserInfo")
	public ModelAndView getUserInfo(HttpServletRequest request, ModelMap map,@RequestParam(value="userid") Integer userid) {
		User user = new User();
		Map<String, Object> model = new HashMap<String, Object>();
		if (userService.selectByPrimaryKey(userid) != null) {
			user = userService.selectByPrimaryKey(userid);
			System.out.println("能查到信息");
			model.put("user", user);
			return new ModelAndView("views/updateuser", model);
		} 
		return new ModelAndView("views/updateuser", model);
	}
	
	//获取用户分类统计
	@RequestMapping(value = "/usercatagory")
	public String getUserCatagory(){
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> categery = new ArrayList<String>();
		List<Long> values = new ArrayList<Long>();
		List<HashMap<String, Object>> maps = userService.countByCategory(null);
		for (int i = 0; i < maps.size(); i++) {
			HashMap<String, Object> hashMap = maps.get(i);
			categery.add((String) hashMap.get("categoryName"));
			values.add((Long) hashMap.get("categoryNum"));
		}
		map.put("categories", categery);
		map.put("values", values.toArray());
		return JSONutils.MapToJson(map);		
	}	
	
	//绘制饼图
	@ResponseBody
	@RequestMapping(value = "getPie", produces = "text/html;charset=utf-8")
	public String getCategoryPie() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> categery = new ArrayList<String>();
		List<String> values = new ArrayList<String>();
		HashMap<String, Object> v = new HashMap<String, Object>();
		v.put("value", "123");
		List<HashMap<String, Object>> maps = userService.countByCategory(v);
		List<HashMap<String, Object>> json = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < maps.size(); i++) {
			HashMap<String, Object> hashMap = maps.get(i);
			HashMap<String, Object> m = new HashMap<String, Object>();
			categery.add((String) hashMap.get("username"));
			values.add((String) hashMap.get("renshu"));
			m.put("name", (String) hashMap.get("username"));
			m.put("value", (String) hashMap.get("renshu"));
			json.add(m);
		}
		map.put("json", json.toArray());
		map.put("value", values.toArray());
		map.put("label", categery.toArray());
		return JSONutils.MapToJson(map);
	}	
}
